﻿using System.Windows.Controls;
using System.Windows.Navigation;
using Fayshine.VA.ViewModels;
using System.Collections.Generic;
using Fayshine.VA.RIAService.Web;
using System.Windows.Controls.Theming;
using System.Windows;

namespace Fayshine.VA.Views.SysConfig
{
    public partial class User : Page
    {
        private UserRoleViewModel m_ViewModel = null;

        public User()
        {
            InitializeComponent();
            this.Loaded += new System.Windows.RoutedEventHandler(User_Loaded);

        }

        private void User_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            m_ViewModel = new UserRoleViewModel();
            this.DataContext = m_ViewModel;
            
            m_ViewModel.GetAllUsers();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void btnAddUser_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            UserWindow newUserWindow = new UserWindow("添加用户");
            newUserWindow.Closed += new System.EventHandler(newUserWindow_Closed);
            newUserWindow.Show();
        }

        void newUserWindow_Closed(object sender, System.EventArgs e)
        {
            UserWindow userWindow = sender as UserWindow;
            if (userWindow.User != null && userWindow.DialogResult.HasValue && userWindow.DialogResult.Value)
            {
                if (!m_ViewModel.ExistUser(userWindow.User.Name))
                {
                    m_ViewModel.AddUser(userWindow.User);
                }
                else
                {
                    MessageBox.Show("用户 " + userWindow.User.Name + " 已经存在！");
                }

            }
        }

        private void btnDeleteUser_Click(object sender, RoutedEventArgs e)
        {
            var selectedUsers = this.dataGrid.GetSelectedItems<user>();
            if (selectedUsers.Count <= 0)
            {
                MessageBox.Show("请选择要删除的用户");
            }
            else
            {
                if (MessageBox.Show("确定要删除选择的用户吗?","确定", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    if (m_ViewModel != null)
                    {
                        m_ViewModel.DeleteUsers(selectedUsers);
                    }
                }
            }
        }

        private void btnUpdateUser_Click(object sender, RoutedEventArgs e)
        {
            user selectedUser = this.dataGrid.SelectedItem as user;
            if (selectedUser == null)
            {
                MessageBox.Show("请选择要编辑的用户");
            }
            else
            {
                UserWindow editUserWindow = new UserWindow("编辑用户",selectedUser);
                editUserWindow.Closed += new System.EventHandler(editUserWindow_Closed);
                editUserWindow.Show();
            }
        }

        void editUserWindow_Closed(object sender, System.EventArgs e)
        {
            UserWindow userWindow = sender as UserWindow;
            if (userWindow.User != null && userWindow.DialogResult.HasValue && userWindow.DialogResult.Value)
            {
                m_ViewModel.Update(userWindow.User);
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (this.m_ViewModel != null)
            {
                this.m_ViewModel.SaveData2();
            }
        }

    }
}
